###############################################################################
# Makefile for the project Lab1-WaveformGenerator
###############################################################################

## General Flags
PROJECT = CameraDriver
MCU = atmega32
TARGET = $(PROJECT).elf
CC = avr-gcc
OBJDUMP=avr-objdump

## Options common to compile, link and assembly rules
COMMON = -mmcu=$(MCU)

## Compile options common for all C compilation units.
CFLAGS = $(COMMON) -g
CFLAGS += -Wall -Os -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums

## Linker flags
LDFLAGS = $(COMMON)
LDFLAGS +=  -Wl


## Intel Hex file production flags
HEX_FLASH_FLAGS = -R .eeprom -R .fuse -R .lock -R .signature

HEX_EEPROM_FLAGS = -j .eeprom
HEX_EEPROM_FLAGS += --set-section-flags=.eeprom="alloc,load"
HEX_EEPROM_FLAGS += --change-section-lma .eeprom=0 --no-change-warnings


## Objects that must be built in order to link
SOURCES=uart.c inoutput.c servo.c twi.c main.c camera.c 
##OBJECTS=$(SOURCES:.c=.o)

## Build
all: $(SOURCES) $(PROJECT).hex

$(TARGET): $(SOURCES)
	$(CC) $(CFLAGS) -o $(TARGET) $(SOURCES)

$(PROJECT).hex: $(TARGET)
	avr-objcopy -O ihex $(HEX_FLASH_FLAGS)  $< $@

%.eep: $(TARGET)
	-avr-objcopy $(HEX_EEPROM_FLAGS) -O ihex $< $@ || exit 0

%.lss: $(TARGET)
	avr-objdump  -S $< > $@

size: ${TARGET}
	@echo
	@avr-size ${TARGET}

install:
	#avrdude -c avrispmkII -p m32 -P usb -U flash:w:$(PROJECT).hex
	avrdude -c jtagmkI -p m32 -P /dev/ttyUSB1 -U flash:w:$(PROJECT).hex

.PHONY: disasm
disasm: all
	$(OBJDUMP) -d -g -h -S $(TARGET)

## Clean target
.PHONY: clean
clean:
	rm -f $(OBJECTS) $(PROJECT).elf $(PROJECT).hex $(PROJECT).lss $(PROJECT).map
